﻿using Common.Consts;
using Common.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace WinServiceClient.Helpers
{
    public class ServiceHelper
    {
        /// <summary>
        /// 判断服务是否存在
        /// </summary>
        /// <param name="serviceName">服务名</param>
        /// <returns></returns>
        public static bool IsServiceExisted(string serviceName)
        {
            var services = ServiceController.GetServices().FirstOrDefault(a=>a.ServiceName.ToLower() == serviceName.ToLower());
            if (services == null)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 安装服务
        /// </summary>
        /// <param name="serviceFilePath">服务的启动路径exe</param>
        public static void InstallService(string serviceFilePath)
        {
            InstallService(serviceFilePath, "");
        }
        public static void InstallService(string serviceFilePath, string serviceName)
        {
            ServiceConst.ServiceName = serviceName;

            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = serviceFilePath;
                IDictionary savedState = new Hashtable();
                installer.Install(savedState);
                installer.Commit(savedState);
            }
        }

        public async static Task UninstallServiceAync(string serviceFilePath)
        {
            await UninstallServiceAync(serviceFilePath, "");
        }
        /// <summary>
        /// 卸载服务
        /// </summary>
        /// <param name="serviceFilePath">服务的启动路径exe</param>
        /// <returns></returns>
        public async static Task UninstallServiceAync(string serviceFilePath, string serviceName)
        {
            ServiceConst.ServiceName = serviceName;

            await Task.Run(() =>
            {
                using (AssemblyInstaller installer = new AssemblyInstaller())
                {
                    installer.UseNewContext = true;
                    installer.Path = serviceFilePath;
                    installer.Uninstall(null);
                }
            });
        }
        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="serviceName">服务名</param>
        public static void ServiceStart(string serviceName)
        {
            using (ServiceController control = new ServiceController(serviceName))
            {
                if (control.Status == ServiceControllerStatus.Stopped)
                {
                    control.Start();
                }
            }
        }

        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="serviceName">服务名</param>
        public static void ServiceStop(string serviceName)
        {
            using (ServiceController control = new ServiceController(serviceName))
            {
                if (control.Status == ServiceControllerStatus.Running)
                {
                    control.Stop();
                }
            }
        }
    }
}
